package com.palfund.secondfragment;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

/**
 * 当Fragment为V4包时,先执行Fragment的onStart()方法再执行Activity的
 * 启动MainActivity
 * MainActivity: ---onCreate()--->First
 * MainActivity: ---onCreate()--->Second
 * MainFragment: ---MainFragment()--->
 * MainFragment: ---onAttach()--->activity
 * MainFragment: ---onAttach()--->context
 * MainFragment: ---onCreate()--->
 * MainFragment: ---onCreateView()--->
 * MainFragment: ---onViewCreated()--->
 * MainFragment: ---onActivityCreated()--->
 * MainActivity: ---onStart()--->
 * MainFragment: ---onStart()--->
 * MainActivity: ---onResume()--->
 * MainFragment: ---onResume()--->
 * <p>
 * 启动SecondActivity
 * MainFragment: ---onPause()--->
 * MainActivity: ---onPause()--->
 * SecondActivity: ---onCreate()--->First
 * SecondActivity: ---onCreate()--->Second
 * SecondFragment: ---SecondFragment()--->
 * SecondFragment: ---onAttach()--->activity
 * SecondFragment: ---onAttach()--->context
 * SecondFragment: ---onCreate()--->
 * SecondFragment: ---onCreateView()--->
 * SecondFragment: ---onViewCreated()--->
 * SecondFragment: ---onActivityCreated()--->
 * SecondActivity: ---onStart()--->
 * SecondFragment: ---onStart()--->
 * SecondActivity: ---onResume()--->
 * SecondFragment: ---onResume()--->
 * MainFragment: ---onStop()--->
 * MainActivity: ---onStop()--->
 * <p>
 * 结束SecondActivity
 * SecondFragment: ---onPause()--->
 * SecondActivity: ---onPause()--->
 * MainActivity: ---onRestart()--->
 * MainActivity: ---onStart()--->
 * MainFragment: ---onStart()--->
 * MainActivity: ---onResume()--->
 * MainFragment: ---onResume()--->
 * SecondFragment: ---onStop()--->
 * SecondActivity: ---onStop()--->
 * SecondFragment: ---onDestroyView()--->
 * SecondFragment: ---onDestroy()--->
 * SecondFragment: ---onDetach()--->
 * SecondActivity: ---onDestroy()--->
 */
public class MainActivity extends AppCompatActivity {

    private FragmentManager mFragmentManager;
    private MainFragment mFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i("MainActivity", "---onCreate()--->First");
        setContentView(R.layout.activity_main);
        Log.i("MainActivity", "---onCreate()--->Second");
        setTitle("MainActivity");
        mFragment = new MainFragment();
        //FragmentManager fragmentManager = getSupportFragmentManager();
        mFragmentManager = getFragmentManager();
        FragmentTransaction transaction = mFragmentManager.beginTransaction();
        transaction.add(R.id.layout_container_main, mFragment);
        transaction.commit();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.i("MainActivity", "---onRestart()--->");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.i("MainActivity", "---onStart()--->");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i("MainActivity", "---onResume()--->");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.i("MainActivity", "---onPause()--->");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.i("MainActivity", "---onStop()--->");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i("MainActivity", "---onDestroy()--->");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(Menu.NONE, 666, 1, "跳转");
        menu.add(Menu.NONE, 000, 1, "add");
        menu.add(Menu.NONE, 888, 2, "remove");
        menu.add(Menu.NONE, 555, 2, "attach");
        menu.add(Menu.NONE, 111, 2, "detach");
        menu.add(Menu.NONE, 333, 3, "show");
        menu.add(Menu.NONE, 999, 4, "hide");
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
        switch (item.getItemId()) {
            case 666:
                startActivity(new Intent(this, SecondActivity.class));
                break;
            case 888:
                fragmentTransaction.remove(mFragment).commit();
                break;
            case 333:
                fragmentTransaction.show(mFragment).commit();
                break;
            case 999:
                fragmentTransaction.hide(mFragment).commit();
                break;
            case 000:
                fragmentTransaction.add(R.id.layout_container_main, mFragment).commit();
                break;
            case 111:
                fragmentTransaction.detach(mFragment).commit();
                break;
            case 555:
                fragmentTransaction.attach(mFragment).commit();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

}
